package com.ysd.lis.controller.eq;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.WkFolder;
import com.ysd.lis.entity.eq.EqEquipment;
import com.ysd.lis.request.eq.EqEquipmentDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.WkFolderService;
import com.ysd.lis.service.eq.EqEquipmentService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备管理-设备类型 前端控制器
 * </p>
 *
 * @author prs
 * @since 2024-07-31
 */
@RestController
@RequestMapping("/api/eq/eqEquipment")
public class EqEquipmentController extends BaseController {

    @Autowired
    private EqEquipmentService eqEquipmentService;
    @Autowired
    SysRuleSetService sysRuleSetService;//编码规则服务

    @Autowired
    WkFolderService wkFolderService;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;


    @ApiOperation("新增或修改")
    @PostMapping("/saveOrUpdateEqEquipment")
    public Result saveOrUpdateEqEquipment(@RequestBody EqEquipment eqEquipments) {
        if (StringUtils.isBlank(eqEquipments.getId())) {
            String a = sysRuleSetService.createRuleCodeList("Equipment", 1).get(0);
            eqEquipments.setEquiptCode(a);
            eqEquipmentService.save(eqEquipments);

            List<Map<String, Object>> fileFolderList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("eqFileFolder").getData();

            List<WkFolder> sbList = wkFolderService.list(new LambdaQueryWrapper<WkFolder>().eq(WkFolder::getCode, "eqEquipment").eq(WkFolder::getDelFlag, 0));
            if (CollectionUtil.isNotEmpty(sbList)) {
                WkFolder sbFolder = sbList.get(0);
                String[] split = sbFolder.getFileFolder().split(",");
                WkFolder wkFolder = new WkFolder();
                wkFolder.setCode(a);
                wkFolder.setName(eqEquipments.getEquiptName() + "(" + a + ")");
                wkFolder.setPid(sbFolder.getId());
                wkFolder.setGrade(0);
                wkFolderService.save(wkFolder);
                if (split.length > 0) {
                    for (String fileFolder : split) {
                        for (Map<String, Object> map : fileFolderList) {
                            if (fileFolder.equals(map.get("code"))) {
                                WkFolder wkFolder1 = new WkFolder();
                                wkFolder1.setCode(a+fileFolder);
                                wkFolder1.setName(map.get("name").toString());
                                wkFolder1.setPid(wkFolder.getId());
                                wkFolder1.setGrade(0);
                                wkFolderService.save(wkFolder1);
                            }
                        }
                    }
                }
            }

        } else {
            eqEquipmentService.updateById(eqEquipments);
        }
        return Result.succ(1, "保存成功", eqEquipments);
    }

    @ApiOperation("获取设备管理信息（分页）")
    @GetMapping("/findEqEquipmentPageList")
    public Result findEqEquipmentPageList(EqEquipmentDto eqEquipmentDto) {
        return eqEquipmentService.findEqEquipmentPageList(eqEquipmentDto);
    }

    @ApiOperation("批量删除")
    @PostMapping("/batchDeleteEqEquipment")
    public Result batchDeleteEqEquipment(@RequestBody List<EqEquipment> eqEquipments) {
        return eqEquipmentService.batchDeleteEqEquipment(eqEquipments);
    }

    @ApiOperation("获取设备管理信息")
    @GetMapping("/findEqEquipmentList")
    public Result findEqEquipmentList(EqEquipmentDto eqEquipmentDto) {
        return eqEquipmentService.findEqEquipmentList(eqEquipmentDto);
    }

    @ApiOperation("同步仪器数据")
    @GetMapping("/synInterData")
    public Result synInterData() {
        return eqEquipmentService.synInterData();
    }
}
